package alpha.com.google.code.mechanic.janitor.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.actions.CompoundContributionItem;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;
import org.eclipse.ui.menus.IWorkbenchContribution;
import org.eclipse.ui.services.IServiceLocator;

import alpha.com.google.code.mechanic.janitor.controller.ContextData;
import alpha.com.google.code.mechanic.janitor.controller.MechanicTaskSource;
import alpha.com.google.code.mechanic.janitor.model.ContextsCollector;
import alpha.com.google.code.mechanic.janitor.model.IContext;

import com.google.eclipse.mechanic.plugin.core.MechanicPlugin;

/**
 * 
 * This class is to dynamically add a drop down list of user defined Contexts to the
 * pull down button that Workspace Janitor added to the Eclipse main toolbar.
 *  
 * @Last_edited: Nov 3, 2014
 *
 * @History
 * 1. In the method initContextsList(), add the code to check whether the
 * @author jingjiejiang
 * @Edited: Nov 3, 2014 
 * directory of the activated Context has been deleted from the "Task sources"
 * in the Workspace Mechanic preference page.
 *
 * @author jingjiejiang
 * @since Sep 24, 2014 
 */
public class ContextSwitchContributionItem extends CompoundContributionItem
    implements IWorkbenchContribution {

  private ContextData contextData = new ContextData();
  private ContextsCollector contextList = null;
  
  private IServiceLocator serviceLocator;
  private long lastTimeStamp = 0;
  
  public ContextSwitchContributionItem () {
  }
  
  public ContextSwitchContributionItem (final String id) {
    super(id);
  }
  
  public void initContextsList () throws Exception {
    
    if (contextList != null) {
      if (contextList.getContextList().size() != 0) {
        contextList.resetContextList();  
      }
    }
    
    try {
      contextData.readContextsFile();
    } catch (Exception e) {
      Shell pshell = Display.getCurrent().getActiveShell();
      MessageDialog.openError(pshell, "Couldn't read the Context data from"
          + " the context.con file.", e.getMessage());
      return;
    }
    
    MechanicTaskSource sources = new MechanicTaskSource();
    sources.checkSelCtxExist(contextData);
    contextData.saveContextsInfo();
    
    contextList = contextData.getContextsCollector();
  }
  
  public void initialize(IServiceLocator serviceLocator) {

    this.serviceLocator = serviceLocator;
  }

  @Override
  protected IContributionItem[] getContributionItems() {
    
    Shell pshell = Display.getCurrent().getActiveShell();
    
    try {
      initContextsList ();
    } catch (Exception e) {
      MessageDialog.openError(pshell, "Couldn't read the Context data."
          , e.getMessage());
      return null;
    }
    
    List<IContributionItem> itemlist = new ArrayList<IContributionItem>();
    lastTimeStamp = System.currentTimeMillis();
        
    Set<IContext> ctxset = contextList.getContextList();
    Iterator<IContext> iterator = ctxset.iterator();

    if (false == iterator.hasNext()) {
      
      CommandContributionItemParameter menuitem = new CommandContributionItemParameter(
          serviceLocator, null, 
          "alpha.com.google.eclipse.mechanic.janitor.ui.command.nocontext",
          CommandContributionItem.STYLE_PUSH);    
      
      menuitem.label = "(none applicable)";
      menuitem.visibleEnabled = true;
      itemlist.add(new CommandContributionItem(menuitem));
    }
    
    // add a list of Contexts.
    while (iterator.hasNext()) {
      
      IContext context = iterator.next();
      CommandContributionItemParameter menuitem = new CommandContributionItemParameter(
          serviceLocator, null, 
          "alpha.com.google.eclipse.mechanic.janitor.ui.command.context",
          CommandContributionItem.STYLE_PUSH);    
      
      menuitem.label = context.getName();
      menuitem.visibleEnabled = true;

      if (context.isSelected()) {
         ImageDescriptor image = MechanicPlugin.getImageDescriptor("icons/ticking_icon.png");
         menuitem.icon = image;
      }
      
      itemlist.add(new CommandContributionItem(menuitem));
    }
    
    // add a option that allow users to switch to the default context
    CommandContributionItemParameter menuitem = new CommandContributionItemParameter(
        serviceLocator, null, 
        "alpha.com.google.eclipse.mechanic.janitor.ui.command.context",
        CommandContributionItem.STYLE_PUSH);    
    
    menuitem.label = "Default Context";
    menuitem.tooltip = "The default Context of the workspace in Eclipse";
    menuitem.visibleEnabled = true;

    itemlist.add(new CommandContributionItem(menuitem));

    return itemlist.toArray(new IContributionItem[itemlist.size()]);
  }
  
  @Override
  public boolean isDirty() {
    
    return lastTimeStamp + 1 < System.currentTimeMillis();
  }

}
